﻿using CnBlogsCore.Model;
using MvvmHelpers;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Input;
using Xamarin.Forms;
using CnBlogsClient.Global;
using System.Threading.Tasks;
using CnBlogsClient.Interface;

namespace CnBlogsClient.ViewModels
{
    class KbArticleViewModel : BaseViewModel
    {
        public ObservableRangeCollection<KbArticle> KbArticles
        {
            get;
        } = new ObservableRangeCollection<KbArticle>();

        public DateTime NextRefreshTime
        {
            get;
            set;
        }

        private int pageIndex = 1;
        private int pageSize = 20;

        public KbArticleViewModel()
        {
            CanLoadMore = false;
        }

        ICommand refreshCommand;
        public ICommand RefreshCommand =>
            refreshCommand ?? (refreshCommand = new Command(async () => 
            {
                try
                {
                    IsBusy = true;
                    NextRefreshTime = DateTime.Now.AddMinutes(15);
                    CanLoadMore = false;
                    pageIndex = 1;
                    await ExecuteRefreshCommandAsync();
                }
                catch (Exception)
                {
                    loadStatus = LoadMoreStatus.StausFail;
                }
                finally
                {
                    IsBusy = false;
                }
                
            }));

        LoadMoreStatus loadStatus;
        public LoadMoreStatus LoadStatus
        {
            get
            {
                return loadStatus;
            }

            set
            {
                SetProperty(ref loadStatus, value);
            }
        }

        ICommand loadMoreCommand;
        public ICommand LoadMoreCommand =>
            loadMoreCommand ?? (loadMoreCommand = new Command(async () => 
            {
                try
                {
                    LoadStatus = LoadMoreStatus.StausLoading;
                    await ExecuteRefreshCommandAsync();

                }
                catch (Exception)
                {
                    LoadStatus = LoadMoreStatus.StausError;
                }
            }));

        async Task ExecuteRefreshCommandAsync()
        {
            List<KbArticle> listData = new List<KbArticle>();
            await Task.Run(()=> {
                listData = DependencyService.Get<IKbArticleService>().GetKbArticles(pageIndex, pageSize);
            });
            
            if (listData.Count > 0)
            {
                if (pageIndex == 1 && KbArticles.Count > 0)
                {
                    KbArticles.Clear();
                }

                KbArticles.AddRange(listData);
                pageIndex++;
                LoadStatus = LoadMoreStatus.StausDefault;
                CanLoadMore = true;
            }
            else
            {
                CanLoadMore = false;
                LoadStatus = pageIndex > 1 ? LoadMoreStatus.StausEnd : LoadMoreStatus.StausNodata;
            }
        }
    }
}
